package com.itjob.deep.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.itjob.deep.bean.TalkBean;
import com.itjob.deep.bean.UserBean;

public class SendServlet extends HttpServlet {
	
	private static final long serialVersionUID = 887733967480106034L;

	@Override
	@SuppressWarnings("unchecked")
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletContext application = this.getServletContext();
		HttpSession session = request.getSession();
		
		String say = request.getParameter("say");
		UserBean user = (UserBean) session.getAttribute("user");
		
		if (user == null) {
			response.getWriter().write("nulluser");
			response.getWriter().flush();
			response.getWriter().close();
		}
		
		List<TalkBean> talks = (List<TalkBean>) application.getAttribute("talks");
		if (talks == null) {
			talks = new ArrayList<TalkBean>();
			application.setAttribute("talks", talks);
		}
		
		TalkBean tb = new TalkBean();
		tb.setSaid(say);
		tb.setUsername(user.getUsername());
		tb.setSendTime(new Date().getTime());
		
		talks.add(tb);
	}
}
